const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    listOrder: [],
    addressee: '',
    addresseePhone: '',
    shipAddress: '',
    addresseeCount: 0,
    note: '',
    //支付
    responseBody: {},
    test: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options.id)
    wx.showToast({
      title: '加载中',
      icon: "loading",
    })
    var that = this;
    that.setData({
      id: options.id,
    });

    this.selectOrder(this.data.id)

  },

  // 提交订单
  bindSubmitOrder: function() {
    var that = this;
    wx.request({
      url: app.globalData.the_host + '/order/wxpay?tenant=t1',
      data: {
        "orderId": that.data.id,
        "billCreateIp": app.globalData.ip //调用微信支付API的机器IP
      },
      method: 'post',
      header: {
        'content-type': 'application/json',
      },
      success: function(res) {
        if (res.data.status == 1) {
          //提交失败
          that.setData({
            test: res.data.data,
          });
          wx.showToast({
            title: `提交失败`,
            duration: 1000
          })
        } else {
          that.setData({
            responseBody: res.data.data,
          });
          that.qurenzhifu(); //先确认支付
        }
       
      },
    })
  },

  //支付
  zhifu: function(e) {
    var that = this;
    console.log(this.data.responseBody)
    that.setData({
      test: that.data.responseBody.timeStamp
    })
    //调起支付
    wx.requestPayment({
      'timeStamp': that.data.responseBody.timeStamp, // '1586710872629',
      'nonceStr': that.data.responseBody.nonce_str, //'gVih75fDfU7LHPqH',
      'package': that.data.responseBody.package, //'prepay_id=wx1301011262605543f11926071917582600',
      'signType': that.data.responseBody.signType, // 'MD5',
      'paySign': that.data.responseBody.paySign, //'A28BF0943A1557090D9856FEF5FA1977',
      'success': function(res) {
        console.log(res)
        that.updataOrder(1); //状态0待付款', "1待发货", "2待收货", "3已完成"
      },
      'fail': function(res) {
        that.updataOrder(0); //状态0待付款', "1待发货", "2待收货", "3已完成"
      },
      'complete': function(res) {
        console.log(res)
      }
    })
  },
  //确认支付
  qurenzhifu: function() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定提交付款？',
      success: function(res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          //点击确定
          if (that.data.addresseeCount > 0) {
            that.zhifu(); //调用支付方法            
            // wx.showToast({
            //   title: `结算成功！`,
            //   icon: 'success',
            //   duration: 500
            // })
          } else {
            //点击确定
            wx.showToast({
              title: `请选中收货地址`,
              duration: 1500
            })
          }
        }
      },
    })
  },
  //选择地址
  btn_address: function() {
    wx.navigateTo({
      url: '../addressList/index'
    })
  },
  //确定支付时 修改当前订单状态
  updataOrder: function(expressStatus) {
    var that = this;

    wx.request({
      url: app.globalData.the_host + `/order/update??tenant=t1`,
      data: {
        "id": that.data.id, //修改id
        "openId": app.globalData.openId, //订单提交人
        "totalPrice": that.data.listOrder.totalPrice, //总价
        "addressee": that.data.addressee, //收货人
        "addresseePhone": that.data.addresseePhone, //收货人号码
        "shipAddress": that.data.shipAddress, //收货地址
        "expressCompany": "", //快递公司
        "expressNum": "", //快车号码
        "expressStatus": expressStatus, //状态0待付款', "1待发货", "2待收货", "3已完成"
        "note": that.data.note, //备注
        "salesChannel": "wx",
        "status": 0,
        "orderDetail": [] //这个可以为空
      },
      method: 'PUT', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.status == 0) {
          //提交成功
          wx.navigateTo({
            url: '../order/index'
          })
        } else {
          //提交失败
          wx.showToast({
            title: `提交失败`,
            duration: 500
          })
        }

      },
    })

  },


  //查询订单列表 找到 当前订单 根据id查找
  selectOrder: function(id) {
    var that = this;
    wx.request({
      url: app.globalData.the_host + `/order/list?tenant=t1`,
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.status == 500) {
          wx.showToast({
            title: `数据获取失败`,
            duration: 1000
          })
          return;
        }
        if (res.data.length > 0) {
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i].id == id) {
              that.setData({
                listOrder: res.data[i]
              });
              wx.hideNavigationBarLoading();
              return;
            }
          }
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.addresslist();
  },
  /**
   * 获取地址
   */
  addresslist: function() {
    var that = this;
    wx.request({
      url: app.globalData.the_host + `/shopping/shipaddresses?openid=` + app.globalData.openId,
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res.data)
        that.setData({
          addresseeCount: res.data.length,
        });
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].isDefault == 1) {
            that.setData({
              addressee: res.data[i].addressee,
              addresseePhone: res.data[i].addresseePhone,
              shipAddress: res.data[i].province + res.data[i].city + res.data[i].district + res.data[i].shipAddress,
            });
            return;
          }
        }
      },
    })
  },
})